﻿using Student2024.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student2024.UI.form
{
    public partial class FormStudent : Form
    {
        //创建学生系统服务实例用于数据库操作
        private readonly StudentSystemService service = new StudentSystemService();
        private string examType;
        /// <summary>
        /// 构造函数，接收考试类型参数并初始化表单
        /// </summary>
        /// <param name="ExamType"></param>
        public FormStudent(string ExamType)
        {
            this.examType = ExamType;
            InitializeComponent();
            //获取制定考试类型的所有学生的记录
            var lst = service.GetAllByExamType(examType);
            //把数据绑定到dataGridView控件上面
            this.dataGridView1.DataSource = lst;
        }

        /// <summary>
        /// 创建新的学生记录的处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            FormCreate frm = new FormCreate(examType);
            var result = frm.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAllByExamType(examType);
                this.dataGridView1.DataSource = lst;
            }
        }

        /// <summary>
        /// 修改学生记录的事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            //获取当前行的Id
            if(this.dataGridView1.CurrentRow != null)
            {
                //获取当前行的id
                Guid id = (Guid)this.dataGridView1.CurrentRow.Cells["Id"].Value;
                FormEdit frm = new FormEdit(id,examType);
                var result = frm.ShowDialog();
                if(result == DialogResult.OK)
                {
                    var lst = service.GetAllByExamType(examType);
                    this.dataGridView1.DataSource=lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];//刷新数据绑定
                    cm.Refresh();
                }
            }
        }

        /// <summary>
        /// 删除学生信息记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //确保当前选中了行
            if (this.dataGridView1.CurrentRow != null)
            {
                //获取当前行的id
                Guid id = (Guid)this.dataGridView1.CurrentRow.Cells["Id"].Value;
                //获取当前行的学生姓名
                string name = (string)this.dataGridView1.CurrentRow.Cells["Name"].Value;
                //弹出确认对话框
                var result = MessageBox.Show($"是否删除【{name}】的信息",
                    "系统提示",
                    MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    //用户确认，调用服务层删除方法
                    service.Delete(id);
                    //重新获取学生记录列表
                    var lst = service.GetAllByExamType(examType);
                    //更新数据源
                    this.dataGridView1.DataSource=lst;
                }
            }
        }

        /// <summary>
        /// 查找某个学生的记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //获取textbox的搜索文本
            string searchTxt = this.txtSearch.Text;
            //清空当前datagridviw视图
            this.dataGridView1.DataSource = null;
            //展示检索后学生信息表
            var lst = StudentSystemService.GetAllByExamTypeAndSearch(examType, searchTxt);
            this.dataGridView1.DataSource = lst;
            //如果返回列表是空值或空表则提示重新输入
            if (lst == null || !lst.Any())
            {
                //如果未检索到输入信息，提示用户重新输入
                var result = MessageBox.Show($"未检索到输入信息，请确认输入是否正确",
                    "系统提示");
                this.dataGridView1.DataSource = service.GetAllByExamType(examType);
            }
        }
    }
}
